package com.atguigu.jxc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.jxc.dao.CustomerReturnListDao;
import com.atguigu.jxc.dao.CustomerReturnListGoodsDao;
import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.*;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.util.AuthContextHolder;
import com.atguigu.jxc.vo.PurchaseListAndReturnListVo;
import com.atguigu.jxc.vo.SaleListAndCustomerReturnList;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerReturnListGoodsServiceImpl implements CustomerReturnListGoodsService {

    @Autowired
    private CustomerReturnListGoodsDao customerReturnListGoodsDao;

    @Autowired
    private CustomerReturnListDao customerReturnListDao;

    @Autowired
    private GoodsDao goodsDao;

    //客户退货单保存
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(CustomerReturnList customerReturnList, String customerReturnListGoodsStr, HttpSession session) {
        //获取登录用户信息
        User userInfo = AuthContextHolder.getUserInfo(session);
        //新增客户退货单
        customerReturnList.setUserId(userInfo.getUserId());
        customerReturnList.setTrueName(userInfo.getTrueName());
        customerReturnListDao.saveCustomerReturnList(customerReturnList);

        //将前台的string转为对象
        List<CustomerReturnListGoods> customerReturnListGoods = JSONObject.parseArray(customerReturnListGoodsStr, CustomerReturnListGoods.class);
        customerReturnListGoods.stream().forEach(item ->{
            //新增客户退货单明细
            item.setCustomerReturnListId(customerReturnList.getCustomerReturnListId());
            customerReturnListGoodsDao.saveCustomerReturnListGoods(item);
            //进货单添加对应添加商品库存
            Goods goods =goodsDao.getGoodsById(item.getGoodsId());
            goods.setInventoryQuantity(goods.getInventoryQuantity()+item.getGoodsNum());
            goods.setState(2);//修改状态为客户退货
            goodsDao.updateGoods(goods);
        });
    }

    //客户退货单查询（可查询条件：退货单号、客户、退款状态）
    @Override
    public Map<String, Object> list(String returnNumber, Integer customerId, Integer state, String sTime, String eTime) {
        //查询
        List<CustomerReturnList> customerReturnListList=customerReturnListDao.listSelect(returnNumber,customerId,state,sTime,eTime);
        Map<String, Object> map= new HashMap<>();
        map.put("rows",customerReturnListList);
        return map;
    }

    //退货单商品信息  Integer customerReturnListId
    @Override
    public Map<String, Object> goodsList(Integer customerReturnListId) {
        //查询
        List<CustomerReturnListGoods> customerReturnListGoodsList=customerReturnListGoodsDao.goodsList(customerReturnListId);
        Map<String, Object> map= new HashMap<>();
        map.put("rows",customerReturnListGoodsList);
        return map;
    }

    //删除退货单 Integer customerReturnListId
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Integer customerReturnListId) {
        if (customerReturnListId!=null){
            //删除退货明细
            customerReturnListGoodsDao.deleteCustomerReturnListGoods(customerReturnListId);
            //删除退货单
            customerReturnListDao.deleteCustomerReturnList(customerReturnListId);
        }
    }

    //支付结算（修改客户退货单退款状态） Integer customerReturnListId
    @Override
    public ServiceVO updateState(Integer customerReturnListId) {
        try {
             //根据id查询退款状态
             CustomerReturnList customerReturnList =customerReturnListDao.selectById(customerReturnListId);
            //修改客户退货单退款状态
             customerReturnList.setState(1);
             customerReturnListDao.updateState(customerReturnList);
             return new ServiceVO(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ServiceVO(ErrorCode.PARA_TYPE_ERROR_CODE,"修改退货单退款状态失败！");
        }
    }

    //客户退货统计（可根据 商品类别、商品编码或名称 条件查询）
    @Override
    public String count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        //  t_customer_return_list t_customer_return_list_goods  t_customer   t_goods_type
        List<SaleListAndCustomerReturnList> customerReturnListVo=customerReturnListDao.count(sTime,eTime,goodsTypeId,codeOrName);
        Gson gson = new Gson();
        //把list转为json
        String json = gson.toJson(customerReturnListVo);
        return json;
    }
}
